package com.fzn.springboot_jsp_shiro.config;

import com.fzn.springboot_jsp_shiro.shiro.cache.RedisCacheManger;
import com.fzn.springboot_jsp_shiro.shiro.realms.CustomerRealm;
import net.sf.ehcache.Ehcache;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author fzn
 * @date 2022/3/19
 * @apiNote
 */
@Configuration
public class ShiroConfig {

    // 1. 创建shiroFilter  负责拦截所有请求
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        // 给filter设置安全管理器
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);

        // 配置系统的受限资源
        Map<String, String> map = new HashMap<>();
        // 不受限的资源要放在上面 设置为公共资源
        map.put("/user/login","anon");
        map.put("/register.jsp","anon");
        map.put("/user/register","anon");
        // anno 表示url可以匿名访问 无需认证和授权
        map.put("/**","authc"); // authc 请求这个资源需要认证和授权


        // 默认的认证界面路径 /login.jsp 默认就是 不写也没事
        shiroFilterFactoryBean.setLoginUrl("/login.jsp");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);

        // 配置系统的公共资源


        return shiroFilterFactoryBean;
    }

    // 2. 创建安全管理器
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(Realm realm) {
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        // 给安全管理器设置Realm
        defaultWebSecurityManager.setRealm(realm);
        return defaultWebSecurityManager;
    }

    // 3.创建自定义realm
    @Bean
    public Realm getRealm(){
        CustomerRealm customerRealm = new CustomerRealm();
        // 修改凭证校验匹配器
        HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
        // 设置加密算法md5
        credentialsMatcher.setHashAlgorithmName("MD5");
        // 设置散列次数
        credentialsMatcher.setHashIterations(1024);
        customerRealm.setCredentialsMatcher(credentialsMatcher);

        // 开启缓存管理器 EhCacheManager shiro默认的缓存
        //customerRealm.setCacheManager(new EhCacheManager());

        // 使用Redis为缓存
        customerRealm.setCacheManager(new RedisCacheManger());
        // 开启全局缓存
        customerRealm.setCachingEnabled(true);
        // 认证缓存
        customerRealm.setAuthenticationCachingEnabled(true);
        customerRealm.setAuthenticationCacheName("authenticationCache");
        // 开启授权缓存
        customerRealm.setAuthorizationCachingEnabled(true);
        customerRealm.setAuthorizationCacheName("authorizationCache");

        return customerRealm;
    }
}
